//
//  GetBalanceCashViewController.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/7/18.
//  Copyright © 2017年 ZhiYu Hao. All rights reserved.
//

#import "GetBalanceCashViewController.h"
#import "LXKRegularExpressionTool.h"
#import "PhotoUpLoadAgreementViewController.h"
#import "WalletViewController.h"
#import "IGWithDrawApi.h"
@interface GetBalanceCashViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *moneyTF;
@property (weak, nonatomic) IBOutlet UITextField *accountTF;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (strong, nonatomic) IBOutlet UILabel *remindLabel;
@property (strong, nonatomic) IBOutlet UIButton *commitBtn;
@property (strong, nonatomic) IBOutlet UIButton *btn;
@property(nonatomic,strong)IGWithDrawApi *withDrawApi;

@end

@implementation GetBalanceCashViewController
#pragma mark -生命周期
- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.title = @"余额提现";
    self.title = NSLocalizedString(@"Balance Withdrawal", nil);
    _commitBtn.frame=CGRectMake(0, 0, GSCALE(300), GSCALE(44));
    _commitBtn.centerX=KSCREENWIDTH/2;
    _commitBtn.bottom=self.view.bottom-GSCALE(200);
    _commitBtn.layer.cornerRadius = 20;
    [_commitBtn addTarget:self action:@selector(commitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _remindLabel.frame=CGRectMake(10, 0, KSCREENWIDTH-20, 20);
    _remindLabel.centerX=_commitBtn.centerX;
    _remindLabel.top=_commitBtn.bottom+10;
    
    _btn.frame=CGRectMake(10, 0, KSCREENWIDTH-20, 30);
    _btn.centerX=_remindLabel.centerX;
    _btn.top=_remindLabel.bottom+10;
    _btn.hidden = ([[IGUser getAuthens] integerValue] == 2) ? YES : NO;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}
-(void)commitBtnClick:(UIButton *)btn{
    if (self.accountTF.text.length == 0)
    {
//        [self presentMessageTips:@"请输入支付宝账号"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Alipay Account", nil)];
        return;
    }
    else if ([LXKRegularExpressionTool isValidateMobile:self.accountTF.text] == NO && [LXKRegularExpressionTool checkEmail:self.accountTF.text] == NO)
    {
//        [self presentMessageTips:@"请输入正确的支付宝账号"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Correct Alipay Account", nil)];

        return;
    }
    else if (self.nameTF.text.length == 0)
    {
//        [self presentMessageTips:@"请输入姓名"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Name", nil)];

        return;
    }
    else if (self.phoneTF.text.length == 0)
    {
//        [self presentMessageTips:@"请输入手机号"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Mobile Number", nil)];

        return;
    }
    else if ([LXKRegularExpressionTool isValidateMobile:self.phoneTF.text] == NO)
    {
//        [self presentMessageTips:@"请输入正确的手机号"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Correct Mobile No.", nil)];

        return;
    }
    else if (self.moneyTF.text.length == 0)
    {
//        [self presentMessageTips:@"请输入金额"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Amount", nil)];

        return;
    }
    else if(self.moneyTF.text.floatValue < 100)
    {
//        [self presentMessageTips:@"提现金额应大于100元"];
        [self presentMessageTips:NSLocalizedString(@"Withdrawal amount should be greater than ￥100 RMB", nil)];

        return;
    }
    else if(self.moneyTF.text.floatValue > _money.floatValue)
    {
//        [self presentMessageTips:[NSString stringWithFormat:@"提现金额应小于%@", _money]];
        [self presentMessageTips:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"Withdrawal Amount<", nil),_money]];

        return;
    }
    [self commitApplication];
    
}

- (void)commitApplication
{
    self.withDrawApi=[[IGWithDrawApi alloc]initWithUid:[IGUser getUid] aliCode:self.accountTF.text name:self.nameTF.text tel:self.phoneTF.text price:self.moneyTF.text];
    [self.withDrawApi startWithDecodeClass:[IGWithDrawResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            [UIViewController ig_popViewController:[WalletViewController class]];
        }
    }];
}
- (IBAction)AuthenticationBtnClick:(UIButton *)sender {
    PhotoUpLoadAgreementViewController *photoAgreenmentVC = [[UIStoryboard storyboardWithName:@"UserCertification" bundle:nil] instantiateViewControllerWithIdentifier:@"PhotoUpLoadAgreementViewController"];
    [UIViewController ig_pushViewController:photoAgreenmentVC animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
